package com.cyx.chapter2.service;

import com.cyx.chapter2.helper.DatabaseHelper;
import com.cyx.chapter2.model.Customer;
import com.cyx.chapter2.util.PropsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/10/7 0007.
 */
public class CustomerService {
    private static final Logger LOGGER=  LoggerFactory.getLogger(PropsUtil.class);
    /**
     * 获取客户列表
     */
    public List <Customer> getCustomerList() {
        Connection conn=DatabaseHelper.getConnect();
        try{
            String sql="select * from customer";
            return DatabaseHelper.queryEntityList(Customer.class,sql);
        }
        catch (Exception e){
            LOGGER.error("getCustomerList failure",e);
        }
        return null;
    }

    /**
     * 获取客户
     */
    public Customer getCustomer(long id) {
        String sql="select * from customer where id="+id;
        return DatabaseHelper.queryEntity(Customer.class,sql);
    }

    /**
     * 创建客户
     */
    public boolean createCustomer(Map <String, Object> fieldMap) {

        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }

    /**
     * 更新客户
     */
    public boolean updateCustomer(long id, Map <String, Object> fieldMap) {

        return DatabaseHelper.updateEntity(Customer.class,id,fieldMap);
    }

    /**
     * 删除用户
     */
    public boolean deleteCustomer(long id) {
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }

}
